﻿using System.Reflection;
using net.entity.framework;
using net.entity.framework.common;
using net.entity.framework.query;
using net.entity.sandBag.entities;
using net.entity.sandBag.plumbing;

namespace net.entity.sandBag.entities {
    public partial class ContactRef {
        public static EqlContactRef QueryRoot {
            get { return new EqlContactRef(new AliasInfo("Mayank", "Contact", typeof (Contact))); }
        }

        /// <summary>
        /// Gets the target Contact on-demand.
        /// </summary>
        /// <para>Probing order:
        /// <list> 
        /// 1. Refrence stored in TyObjRef
        /// 2. LookupAlias TypeIdCache
        /// 3. Load the chunk (5000) from database
        /// </list>
        /// </para>
        public Contact T {
            get {
                lock (syncLock) {
                    if (Target == null) {
                        Target = Resolve<Contact>();
                    }
                    return Target as Contact;
                }
            }
        }

        public ContactRef(int id, int typeId, int revision) : base(id, typeId, revision) {}

        public ContactRef(int id, int typeId, int revision, QueryContext queryContext)
            : base(id, typeId, revision, queryContext) {}
    }

    public partial class Contact {
        public ContactRef AutoRef {
            get { return EntityInvoker.InvokeEntityRef<ContactRef>(this); }
        }

        public Contact UnlockCloneForRevise() {
            return UnlockCloneForRevise<Contact>();
        }
    }
}

namespace net.entity.sandBag.plumbing {
    public class EqlContactRef : EqlEntityRef {
        public EqlContact T {
            get { return new EqlContact(aliasInfo); }
        }

        public EqlContactRef(AliasInfo aliasInfo) : base(aliasInfo) { }

        public EqlContactRef(AliasInfo aliasInfo, PropertyInfo propertyInfo) : base(aliasInfo, propertyInfo) {}
    }

    public class EqlContact : EqlEntity {
        public EqlContact(AliasInfo aliasInfo) : base(aliasInfo) {}

        public Terminal Name {
            get {
                return Terminal.Create(aliasInfo, "name", typeof (string),
                                       Utils.GetPropertyInfoFromExample(() => ((Contact) null).Name));
            }
        }

        public EqlRoleRef RoleRef {
            get {
                return new EqlRoleRef(new AliasInfo("Mayank", "Role", typeof (RoleRef), "role", aliasInfo),
                                      Utils.GetPropertyInfoFromExample(() => ((Contact) null).RoleRef));
            }
        }
    }
}